//
//  ByCellIndexFuction.m
//  chequanbao_ios
//
//  Created by mc on 2017/10/24.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellIndexFuction.h"

// vc
#import "ByUIShopsNewIndexVC.h"
#import "ByUIPropertySafetyVC.h"
#import "ByUIStoreIndexVC.h"
#import "ByUIAppIndexDevelopenttVC.h"
#import "ByUIAppIndexMoreVC.h"

@implementation ByCellIndexFuction

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByCellIndexFuction";
    ByCellIndexFuction *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        self.backgroundColor = [UIColor clearColor];
        [self createViews];
    }
    return self;
}

- (void)createViews{
    [self createFuncationView];
    [self createOtherView];
}
- (void)createFuncationView {
    
    CGFloat x, y, width, height;
    NSArray *images = @[@"Index_shangjia",
                        @"Index_baoxian",
                        @"Index_shangcheng",
                        @"Index_dingzhi",
                        ];
    
    NSArray *texts = @[@"联盟商家",
                       @"有车一族",
                       @"唯优商城",
                       @"掌柜之家",
                       ];
 
    CGFloat m_h, m_v;
    m_h = 10;
    m_v = 10;
    
    for (int i = 0; i < texts.count; i ++) {
        width = (SCREEN_WIDTH - 3 * m_h) / 2;
        height = width / 335 * 160;
        x = m_h + (i % 2) * (width + m_h);
        y = m_v + (i / 2) * (height + m_v);
        UIControl *view = [self createHorizontalModeView:CGRectMake(x, y, width, height)
                                               imageSize:CGSizeMake(height * 0.5, height * 0.5)
                                               imageName:images[i]
                                                textFont:FONT(18)
                                                    text:texts[i]
                                                  margin:7];
        view.layer.cornerRadius = 4;
        view.tag = 1 + i;
        [self.contentView addSubview:view];
        
        
        [view addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
}

- (void)createOtherView {
    
    CGFloat x, y, width, height, margin = 10;
    x = margin;
    y = self.contentView.subviews.lastObject.bottom + margin;
    width = SCREEN_WIDTH - margin * 2;
    height = width / 690 * 160;
    // 1.创建基底视图
    UIControl *basalView = [[UIControl alloc] initWithFrame:CGRectMake(x, y, width, height)];
    basalView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    basalView.layer.cornerRadius = 4;
    [self.contentView addSubview:basalView];
    basalView.tag = 100;
    [basalView addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    
  
    // 3.创建文字
    UIFont *font = FONT(16);
    NSString *text = @"更多美好";
    width = basalView.width;
    height = basalView.height;
    x = 0;
    y = 0;
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    textLabel.font = font;
    textLabel.text = text;
    textLabel.textColor = [UIColor whiteColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    [basalView addSubview:textLabel];
    
  
}

// 创建水平排版的视图（可点击）
- (UIControl *)createHorizontalModeView:(CGRect)frame
                              imageSize:(CGSize)size
                              imageName:(NSString *)name
                               textFont:(UIFont *)font
                                   text:(NSString *)text
                                 margin:(CGFloat)margin {
    
    // 1.创建基底视图
    UIControl *basalView = [[UIControl alloc] initWithFrame:frame];
    basalView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    
 
    // 2.创建图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    imageView.image = [UIImage imageNamed:name];
    [basalView addSubview:imageView];
    
    
    // 3.创建文字
    CGFloat fontSize = [font pointSize];
    CGFloat width = [text getSizeOfString:fontSize].width + 2; // 防止iOS10以上不适配
    CGFloat height = basalView.height;
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    textLabel.numberOfLines = 0;
    textLabel.font = font;
    textLabel.text = text;
    textLabel.textColor = [UIColor whiteColor];
    textLabel.textAlignment = NSTextAlignmentCenter;
    [basalView addSubview:textLabel];
    
    // 4.适配距离
    imageView.right = basalView.width / 2 - 20;
    imageView.y = basalView.height / 2;
    textLabel.left = imageView.right + margin;
    textLabel.y = basalView.height / 2;
    
    return basalView;
}
#pragma mark - 点击事件
- (void)tapAction:(UIControl *)sender {
    
    
    switch (sender.tag) {
        case 1:
        {
            ByUIShopsNewIndexVC *vc = [[ByUIShopsNewIndexVC alloc] init];
            [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
        }
            break;
        case 2:
        {
            if (!BY_USER) {
                ByUILoginVC *vc = [[ByUILoginVC alloc] init];
                ByBaseNaviController *navi = [[ByBaseNaviController alloc] initWithRootViewController:vc];
                [(ByBaseVC *)self.viewController presentViewController:navi animated:YES completion:nil];
                return;
            }

            ByUIPropertySafetyVC *vc = [[ByUIPropertySafetyVC alloc] init];
            [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
        }
            break;
        case 3:
        {
            ByUIStoreIndexVC *vc = [[ByUIStoreIndexVC alloc] init];
            [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
        }
            break;
      
        case 4:
        {
//            ByUIAppIndexDevelopenttVC *vc = [[ByUIAppIndexDevelopenttVC alloc] initWithTitle:@"创新消费新模式 共享定制新生活" imageName:@"Index_dev_dingzhi" content:@"        生活中，能用5000元买到的东西我们绝不想花7000元的支出，如何能以最优惠的价格获得心仪的产品又不致卖方减少利润？这是一种消费新观念。作为消费者，我们的需求很多，但刚需方面大都类似，如果因为价高而放弃某些钟爱的东西，实在是惋惜。\n        为解决供需价格问题，江苏掌握信息科技独创了一套提前定制商业推广模式，帮助有共同需求的会员一起来省钱，以最实惠的价格拿下心仪的产品和服务。如您想买一台30万的车，4S店告诉您这30万的价格已经是全国范围内能给到的最低折扣，但您只有22万，这时您就可以通过掌握平台发起同款类型车的购车愿意，当会员需求累计达到目标之后，掌握平台即可与汽车生产企业进行沟通，从而定制生产。一方面减少了会员消费开支，另一方面帮助生产企业去掉库存和消化原材料存贮等，创造互利共赢的良好消费生态市场，同时消费者的需求可延伸到生活方方面面的需求，掌握科技减少消费者消费负担而让生产企业获得更多利润。\n        传统的消费模式是被动接受产品，但是如果厂家提前知道消费者需求、喜好、创意等等，则会生产出专属某些群体的独创产品，供需水平达到最优化，省去中间费，消费者花更少钱，生产商赚更多钱，互惠双赢的同时打造精致生活的完美享受，共享掌握定制带来的新惊喜。\n        掌握定制，总有一款适合你。" bottomImgName:nil];
//            [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
            ByUIAppIndexDevelopenttVC *vc = [[ByUIAppIndexDevelopenttVC alloc] initWithTitle:@"消费分红天天有 余额增值日日升" imageName:@"Index_dev_licai" content:@"        理财是人们在不确定环境中进行资源跨期的最优配置决策的行为，是对现有资源进行重新整合，实现价值和利润的等效流通。随着经济生活水平的提高，随之带来的生活压力使很多人慢慢学会精打细算，理财渐渐成为越来越多人的生活内容，从银行存钱到花钱炒股，都是期望获得额外的经济收入。何时能够不敲着算盘过日子、不在乎股票涨停、任性的坐等收入？\n        大连掌握信息科技项目创始人黄锋独创的“消费红利主动增值及再次分配”模式就是最好的懒人理财，只要有消费，就会有分红；消费越多，所获分红越大；如您去掌握平台联盟饭店消费1000元（已经是折扣价），那么您此次获得消费分红1000*5%/2=25元，这25元即可进入您的账户，每次消费红利均可累积，有消费就有分红并且可增值，日积月累便是财富，这样的理财能实实在在看到结果，让理财变成一件容易到无需学习的事。\n        大连掌握信息科技，伴您余额增值日日升。" bottomImgName:nil];
            [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];

        }
            break;
   
        case 100:
        {
            ByUIAppIndexMoreVC *vc = [[ByUIAppIndexMoreVC alloc] init];
            [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
        }
            break;
        default:
            break;
    }
}
@end
